{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#  一、案例简介\n",
    "## 1. 案例背景\n",
    ">   随着时代的进步，航空航天作为我国的战略性发展事业，取得了极大成就。航天事业的发展核心是飞行安全，飞行安全既是飞行人员与乘客的生命安全保障，又是航天科技发展的方向和目标。气象条件对飞行的影响是不同的，也是不可避免的，航天部门应对飞行威胁较大的恶劣天气进行分析，采取相应的对策进行防范，不断提高飞行的安全性与可靠性。\n",
    ">   恶劣天气的类型很多，主要包括大风、云、雷电、暴雨、冰雪，另外，大雾、风切变、冰高空急流等也会对飞行安全产生不同程度的影响。恶劣天气的影响具有不确定性，因而应减少在恶劣天气范围内进行飞行。\n",
    ">   通过天气因素上座数量的分析，预测不同天气情况下航班的上座情况，进而调整飞行计划以应对不同天气带来的影响。\n",
    "\n",
    "## 2. 案例意义\n",
    ">   从分析结果将使航空公司依据天气模式调整飞行时间表。它也可以引导乘客做出新的选择。航空公司可以提前几天预测到未来航班上座情况，然后未雨绸缪地进行调整。航空公司也可以预先发出警告更有效分配自己的资源、地勤人员、飞行人员和其他资产以减少损失\n",
    "## 3. 业务图\n",
    "![业务图](image-001.png)\n",
    "## 4. 案例目标\n",
    ">     现在，大多数航班只能对连续进行重复，一般处理天气延误的航班是在其发生之后。我们的大数据能够让他们预先预测延迟，以更好地与乘客沟通，以及优化资源。通过数据分析，预测假设航班和不同天气情况之间的结果，帮助迅速提前发现因天气发生的上座影响。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 二、相关技术\n",
    "- ## 数据采集，通过pymysql连接mysql数据库获取数据 \n",
    "- ## 数据预处理，通过pandas、sklearn进行数据清洗，转换等预处理\n",
    "- ## 数据探索，通过matplotlib及pyecharts进行数据可视化，发现数据规律\n",
    "- ## 数据分析建模，通过机器学习算法库sklearn学习已有数据规律建模，预测指定航班在不同天气下的上座率"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 三、案例步骤"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- ## 1. 数据采集: 从数据库casepro中获取数据表t_plane_order\\t_plane_weather,将表格转存在本地csv并展示数据\n",
    "\n",
    "> 数据库信息： host：10.102.52.248，port=3306， user=\"root\", passwd=\"root\"\n",
    "\n",
    "> 航班订单数据，包含航班时刻表，航班号，子订单，飞行日期，头等舱，公务舱，经济舱，其他，头等舱总数，公务舱总数，经济舱总数，其他总数。\n",
    "\n",
    "> 天气数据，包含基线：日期，高温，低温，天气状况，风，空气"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pymysql #导入模块\n",
    "import pandas as pd\n",
    "\n",
    "#远程连接数据库\n",
    "db = pymysql.connect(\n",
    "         host='10.102.52.248',\n",
    "         port=3306,\n",
    "         user='root',\n",
    "         passwd='root',\n",
    "         db='casepro',\n",
    "         charset='utf8'\n",
    "         )\n",
    "def link_mysql(db):\n",
    "    #使用cursor()方法获取操作游标 \n",
    "    cursor = db.cursor()\n",
    "    sql = \"\"\"SELECT * FROM `t_plane_order`\"\"\"\n",
    "    sql1 = \"\"\"SELECT * FROM `t_plane_weather`\"\"\"\n",
    "    try:\n",
    "        cursor.execute(sql)  # 执行sql语句\n",
    "        result = cursor.fetchall()\n",
    "        columns = ['航班时刻表','航班号','子订单','日期','头等舱','公务舱','经济舱','其他','头等舱总数','公务舱总数','经济舱总数','其他总数']\n",
    "        frame = pd.DataFrame(list(result),columns=columns)\n",
    "        frame.to_csv(\"航班天气因素上座情况预测分析案例.csv\",encoding='utf-8')\n",
    "        cursor.execute(sql1)  # 执行sql语句\n",
    "        result1 = cursor.fetchall()\n",
    "        columns_weather = ['日期','高温','低温','天气状况','风','空气']\n",
    "        frame_weather = pd.DataFrame(list(result1),columns=columns_weather)\n",
    "        frame_weather.to_csv(\"天气数据.csv\",encoding='utf-8')\n",
    "    except Exception:\n",
    "        db.rollback()  # 发生错误时回滚\n",
    "        print(\"查询失败\")\n",
    "    cursor.close()  # 关闭游标\n",
    "    db.close()  # 关闭数据库连接\n",
    "link_mysql(db)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- ## 2、数据预处理（清洗、数值化转换、标准化等）\n",
    "\n",
    "> 空数据判断和处理（注意分析空占比，区分数值型和类别型）\n",
    "\n",
    "> 数据规范性检查（格式、范围等）\n",
    "\n",
    "> 去除不规范数据、无效、无分析价值数据等"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "#对航班数据进行清理\n",
    "def yucli_plane():\n",
    "    df_plane = pd.read_csv('航班天气因素上座情况预测分析案例.csv',encoding='utf-8')\n",
    "    df_plane = pd.DataFrame(data=df_plane)\n",
    "    df_plane = df_plane.drop(columns='Unnamed: 0')\n",
    "    #判断其空占比\n",
    "    df_plane_percent = df_plane.isna().sum().sort_values(ascending=False) / len(df_plane)\n",
    "    print(\"各字段的空占比为：\\n{}\".format(df_plane_percent))\n",
    "    #删除空数据\n",
    "    df_plane_notNu = df_plane.dropna(axis=0)\n",
    "    #删除重复数据\n",
    "    df_plane_notNu = df_plane_notNu.drop_duplicates()\n",
    "    #将str类型改为float\n",
    "    for i in range(4,12):\n",
    "        df_plane_notNu.iloc[:,i] = df_plane_notNu.iloc[:,i].astype(float)\n",
    "    #将负值替换成零\n",
    "    for i in range(4,12):\n",
    "        df_plane_notNu.iloc[:,i][df_plane_notNu.iloc[:,i] < 0] = 0.0\n",
    "    #删除日期中不规范的数据\n",
    "    df_plane_notNu[df_plane_notNu.iloc[:,3] == \"0.0\"] = np.nan\n",
    "    df_plane_notNu.dropna(axis=0)\n",
    "    return df_plane_notNu\n",
    "\n",
    "#对天气数据进行处理\n",
    "def yucli_weath():\n",
    "    df_weath = pd.read_csv('天气数据.csv',encoding='utf-8')\n",
    "    df_weath = pd.DataFrame(df_weath)\n",
    "    #删除无分析价值数据\n",
    "    df_weath = df_weath.drop('Unnamed: 0',axis=1)\n",
    "    #将日期和空气格式化\n",
    "    df_weath['日期'] = df_weath['日期'].apply(lambda x:str(x)[0:10])\n",
    "    df_weath['空气'] = df_weath['空气'].apply(lambda x:str(x)[:-1])\n",
    "    return df_weath"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\python\\lib\\site-packages\\IPython\\core\\interactiveshell.py:3249: DtypeWarning: Columns (3,5) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  if (await self.run_code(code, result,  async_=asy)):\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "各字段的空占比为：\n",
      "子订单      0.272959\n",
      "航班号      0.013529\n",
      "日期       0.000389\n",
      "其他总数     0.000000\n",
      "经济舱总数    0.000000\n",
      "公务舱总数    0.000000\n",
      "头等舱总数    0.000000\n",
      "其他       0.000000\n",
      "经济舱      0.000000\n",
      "公务舱      0.000000\n",
      "头等舱      0.000000\n",
      "航班时刻表    0.000000\n",
      "dtype: float64\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\python\\lib\\site-packages\\ipykernel_launcher.py:21: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>航班时刻表</th>\n",
       "      <th>头等舱</th>\n",
       "      <th>公务舱</th>\n",
       "      <th>经济舱</th>\n",
       "      <th>其他</th>\n",
       "      <th>头等舱总数</th>\n",
       "      <th>公务舱总数</th>\n",
       "      <th>经济舱总数</th>\n",
       "      <th>其他总数</th>\n",
       "      <th>高温</th>\n",
       "      <th>低温</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>日期</th>\n",
       "      <th>航班号</th>\n",
       "      <th>天气状况</th>\n",
       "      <th>风</th>\n",
       "      <th>空气</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td rowspan=\"5\" valign=\"top\">2016-01-02</td>\n",
       "      <td>8160</td>\n",
       "      <td>霾~雾</td>\n",
       "      <td>无持续风向微风</td>\n",
       "      <td>严重污染</td>\n",
       "      <td>1615205.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>324.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>-8.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>9581</td>\n",
       "      <td>霾~雾</td>\n",
       "      <td>无持续风向微风</td>\n",
       "      <td>严重污染</td>\n",
       "      <td>2297904.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>15.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>24.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>450.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>15.0</td>\n",
       "      <td>-12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>BR715</td>\n",
       "      <td>霾~雾</td>\n",
       "      <td>无持续风向微风</td>\n",
       "      <td>严重污染</td>\n",
       "      <td>4525245.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>195.0</td>\n",
       "      <td>1190.0</td>\n",
       "      <td>280.0</td>\n",
       "      <td>25.0</td>\n",
       "      <td>-20.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>CA101</td>\n",
       "      <td>霾~雾</td>\n",
       "      <td>无持续风向微风</td>\n",
       "      <td>严重污染</td>\n",
       "      <td>400218.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>48.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>483.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>15.0</td>\n",
       "      <td>-12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>CA107</td>\n",
       "      <td>霾~雾</td>\n",
       "      <td>无持续风向微风</td>\n",
       "      <td>严重污染</td>\n",
       "      <td>404502.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>32.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>322.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>15.0</td>\n",
       "      <td>-12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td rowspan=\"5\" valign=\"top\">2016-12-31</td>\n",
       "      <td>ZH9157</td>\n",
       "      <td>霾</td>\n",
       "      <td>南风1-2级</td>\n",
       "      <td>重度污染</td>\n",
       "      <td>3144374.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>15.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>24.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>450.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>-20.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>ZH9159</td>\n",
       "      <td>霾</td>\n",
       "      <td>南风1-2级</td>\n",
       "      <td>重度污染</td>\n",
       "      <td>3949220.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>24.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>450.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>-25.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>ZH9163</td>\n",
       "      <td>霾</td>\n",
       "      <td>南风1-2级</td>\n",
       "      <td>重度污染</td>\n",
       "      <td>3068798.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>45.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>24.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>450.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>-20.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>ZH9165</td>\n",
       "      <td>霾</td>\n",
       "      <td>南风1-2级</td>\n",
       "      <td>重度污染</td>\n",
       "      <td>3072106.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>24.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>450.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>-20.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>ZH9168</td>\n",
       "      <td>霾</td>\n",
       "      <td>南风1-2级</td>\n",
       "      <td>重度污染</td>\n",
       "      <td>3083458.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>15.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>24.0</td>\n",
       "      <td>480.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>-20.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>200655 rows × 11 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                         航班时刻表  头等舱  公务舱   经济舱   其他  头等舱总数  \\\n",
       "日期         航班号    天气状况 风       空气                                            \n",
       "2016-01-02 8160   霾~雾  无持续风向微风 严重污染  1615205.0  0.0  0.0   0.0  0.0   16.0   \n",
       "           9581   霾~雾  无持续风向微风 严重污染  2297904.0  0.0  0.0  15.0  0.0   24.0   \n",
       "           BR715  霾~雾  无持续风向微风 严重污染  4525245.0  0.0  0.0   8.0  3.0    0.0   \n",
       "           CA101  霾~雾  无持续风向微风 严重污染   400218.0  0.0  0.0   0.0  0.0   48.0   \n",
       "           CA107  霾~雾  无持续风向微风 严重污染   404502.0  0.0  0.0   2.0  0.0   32.0   \n",
       "...                                        ...  ...  ...   ...  ...    ...   \n",
       "2016-12-31 ZH9157 霾    南风1-2级  重度污染  3144374.0  0.0  0.0  15.0  0.0   24.0   \n",
       "           ZH9159 霾    南风1-2级  重度污染  3949220.0  0.0  0.0   0.0  0.0   24.0   \n",
       "           ZH9163 霾    南风1-2级  重度污染  3068798.0  3.0  0.0  45.0  0.0   24.0   \n",
       "           ZH9165 霾    南风1-2级  重度污染  3072106.0  0.0  0.0  10.0  0.0   24.0   \n",
       "           ZH9168 霾    南风1-2级  重度污染  3083458.0  0.0  0.0  15.0  0.0    0.0   \n",
       "\n",
       "                                     公务舱总数   经济舱总数   其他总数    高温    低温  \n",
       "日期         航班号    天气状况 风       空气                                      \n",
       "2016-01-02 8160   霾~雾  无持续风向微风 严重污染    0.0   324.0    0.0  10.0  -8.0  \n",
       "           9581   霾~雾  无持续风向微风 严重污染    0.0   450.0    0.0  15.0 -12.0  \n",
       "           BR715  霾~雾  无持续风向微风 严重污染  195.0  1190.0  280.0  25.0 -20.0  \n",
       "           CA101  霾~雾  无持续风向微风 严重污染    0.0   483.0    0.0  15.0 -12.0  \n",
       "           CA107  霾~雾  无持续风向微风 严重污染    0.0   322.0    0.0  15.0 -12.0  \n",
       "...                                    ...     ...    ...   ...   ...  \n",
       "2016-12-31 ZH9157 霾    南风1-2级  重度污染    0.0   450.0    0.0  16.0 -20.0  \n",
       "           ZH9159 霾    南风1-2级  重度污染    0.0   450.0    0.0  20.0 -25.0  \n",
       "           ZH9163 霾    南风1-2级  重度污染    0.0   450.0    0.0  16.0 -20.0  \n",
       "           ZH9165 霾    南风1-2级  重度污染    0.0   450.0    0.0  16.0 -20.0  \n",
       "           ZH9168 霾    南风1-2级  重度污染   24.0   480.0    0.0  16.0 -20.0  \n",
       "\n",
       "[200655 rows x 11 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_result = pd.merge(left=yucli_plane(), right=yucli_weath(), on=\"日期\", how=\"inner\")\n",
    "df_result.groupby(['日期','航班号','天气状况','风','空气']).sum()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- ## 3、数据探索，发现数据中的规律"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 331,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>航班时刻表</th>\n",
       "      <th>头等舱</th>\n",
       "      <th>公务舱</th>\n",
       "      <th>经济舱</th>\n",
       "      <th>其他</th>\n",
       "      <th>头等舱总数</th>\n",
       "      <th>公务舱总数</th>\n",
       "      <th>经济舱总数</th>\n",
       "      <th>其他总数</th>\n",
       "      <th>高温</th>\n",
       "      <th>低温</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>风</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>1-2级风</td>\n",
       "      <td>8.711607e+10</td>\n",
       "      <td>91759.0</td>\n",
       "      <td>50408.0</td>\n",
       "      <td>604719.0</td>\n",
       "      <td>3551.0</td>\n",
       "      <td>1790753.0</td>\n",
       "      <td>1227866.0</td>\n",
       "      <td>27988397.0</td>\n",
       "      <td>813138.0</td>\n",
       "      <td>4822472.0</td>\n",
       "      <td>2748166.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3-4级风</td>\n",
       "      <td>2.163105e+10</td>\n",
       "      <td>21415.0</td>\n",
       "      <td>10714.0</td>\n",
       "      <td>145010.0</td>\n",
       "      <td>709.0</td>\n",
       "      <td>460048.0</td>\n",
       "      <td>317345.0</td>\n",
       "      <td>7187863.0</td>\n",
       "      <td>208877.0</td>\n",
       "      <td>755096.0</td>\n",
       "      <td>241327.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4-5级风</td>\n",
       "      <td>7.801081e+09</td>\n",
       "      <td>8055.0</td>\n",
       "      <td>3315.0</td>\n",
       "      <td>49482.0</td>\n",
       "      <td>258.0</td>\n",
       "      <td>161121.0</td>\n",
       "      <td>116264.0</td>\n",
       "      <td>2566357.0</td>\n",
       "      <td>77414.0</td>\n",
       "      <td>285644.0</td>\n",
       "      <td>92514.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>微风</td>\n",
       "      <td>1.118632e+11</td>\n",
       "      <td>107747.0</td>\n",
       "      <td>56567.0</td>\n",
       "      <td>727642.0</td>\n",
       "      <td>3823.0</td>\n",
       "      <td>2405339.0</td>\n",
       "      <td>1591634.0</td>\n",
       "      <td>36834864.0</td>\n",
       "      <td>1013661.0</td>\n",
       "      <td>3350183.0</td>\n",
       "      <td>553395.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>无持续风向微风</td>\n",
       "      <td>9.011111e+10</td>\n",
       "      <td>97055.0</td>\n",
       "      <td>47581.0</td>\n",
       "      <td>616207.0</td>\n",
       "      <td>3957.0</td>\n",
       "      <td>1848987.0</td>\n",
       "      <td>1360109.0</td>\n",
       "      <td>29630773.0</td>\n",
       "      <td>883349.0</td>\n",
       "      <td>3869962.0</td>\n",
       "      <td>2049819.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  航班时刻表    头等舱   公务舱    经济舱    其他  头等舱总数  \\\n",
       "风                                                                              \n",
       "1-2级风         8.711607e+10   91759.0  50408.0  604719.0  3551.0   1790753.0   \n",
       "3-4级风         2.163105e+10   21415.0  10714.0  145010.0   709.0    460048.0   \n",
       "4-5级风         7.801081e+09    8055.0   3315.0   49482.0   258.0    161121.0   \n",
       "微风            1.118632e+11  107747.0  56567.0  727642.0  3823.0   2405339.0   \n",
       "无持续风向微风  9.011111e+10   97055.0  47581.0  616207.0  3957.0   1848987.0   \n",
       "\n",
       "                公务舱总数  经济舱总数   其他总数       高温       低温  \n",
       "风                                                                       \n",
       "1-2级风          1227866.0  27988397.0   813138.0  4822472.0  2748166.0  \n",
       "3-4级风           317345.0   7187863.0   208877.0   755096.0   241327.0  \n",
       "4-5级风           116264.0   2566357.0    77414.0   285644.0    92514.0  \n",
       "微风             1591634.0  36834864.0  1013661.0  3350183.0   553395.0  \n",
       "无持续风向微风   1360109.0  29630773.0   883349.0  3869962.0  2049819.0  "
      ]
     },
     "execution_count": 331,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_result = df_result.replace(['东北风微风','东南风微风','东风微风','北风微风','南风微风','西北风微风','西南风微风','西风微风'],'微风')\n",
    "df_result = df_result.replace(['东北风1-2级','东南风1-2级','东风1-2级','北风1-2级','南风1-2级','西北风1-2级','西南风1-2级','西风1-2级'],'1-2级风')\n",
    "df_result = df_result.replace(['东风3-4级','北风3-4级','南风3-4级','西北风3-4级'],'3-4级风')\n",
    "df_result = df_result.replace('北风4-5级','4-5级风')\n",
    "df_result2=df_result.groupby('风').sum()\n",
    "df_result2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 332,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<script>\n",
       "    require.config({\n",
       "        paths: {\n",
       "            'echarts':'https://assets.pyecharts.org/assets/echarts.min'\n",
       "        }\n",
       "    });\n",
       "</script>\n",
       "\n",
       "        <div id=\"870f13a2d0df45678425a462ef5a9ceb\" style=\"width:900px; height:500px;\"></div>\n",
       "\n",
       "<script>\n",
       "        require(['echarts'], function(echarts) {\n",
       "                var chart_870f13a2d0df45678425a462ef5a9ceb = echarts.init(\n",
       "                    document.getElementById('870f13a2d0df45678425a462ef5a9ceb'), 'white', {renderer: 'canvas'});\n",
       "                var option_870f13a2d0df45678425a462ef5a9ceb = {\n",
       "    \"animation\": true,\n",
       "    \"animationThreshold\": 2000,\n",
       "    \"animationDuration\": 1000,\n",
       "    \"animationEasing\": \"cubicOut\",\n",
       "    \"animationDelay\": 0,\n",
       "    \"animationDurationUpdate\": 300,\n",
       "    \"animationEasingUpdate\": \"cubicOut\",\n",
       "    \"animationDelayUpdate\": 0,\n",
       "    \"color\": [\n",
       "        \"#CD5C5C\",\n",
       "        \"#FFDAB9\",\n",
       "        \"#228B22\",\n",
       "        \"#c23531\",\n",
       "        \"#2f4554\",\n",
       "        \"#61a0a8\",\n",
       "        \"#d48265\",\n",
       "        \"#749f83\",\n",
       "        \"#ca8622\",\n",
       "        \"#bda29a\",\n",
       "        \"#6e7074\",\n",
       "        \"#546570\",\n",
       "        \"#c4ccd3\",\n",
       "        \"#f05b72\",\n",
       "        \"#ef5b9c\",\n",
       "        \"#f47920\",\n",
       "        \"#905a3d\",\n",
       "        \"#fab27b\",\n",
       "        \"#2a5caa\",\n",
       "        \"#444693\",\n",
       "        \"#726930\",\n",
       "        \"#b2d235\",\n",
       "        \"#6d8346\",\n",
       "        \"#ac6767\",\n",
       "        \"#1d953f\",\n",
       "        \"#6950a1\",\n",
       "        \"#918597\"\n",
       "    ],\n",
       "    \"series\": [\n",
       "        {\n",
       "            \"type\": \"bar\",\n",
       "            \"name\": \"\\u5934\\u7b49\\u8231\",\n",
       "            \"legendHoverLink\": true,\n",
       "            \"data\": [\n",
       "                91759.0,\n",
       "                21415.0,\n",
       "                8055.0,\n",
       "                107747.0,\n",
       "                97055.0\n",
       "            ],\n",
       "            \"showBackground\": false,\n",
       "            \"barMinHeight\": 0,\n",
       "            \"barCategoryGap\": \"20%\",\n",
       "            \"barGap\": \"30%\",\n",
       "            \"large\": false,\n",
       "            \"largeThreshold\": 400,\n",
       "            \"seriesLayoutBy\": \"column\",\n",
       "            \"datasetIndex\": 0,\n",
       "            \"clip\": true,\n",
       "            \"zlevel\": 0,\n",
       "            \"z\": 2,\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"top\",\n",
       "                \"margin\": 8\n",
       "            },\n",
       "            \"rippleEffect\": {\n",
       "                \"show\": true,\n",
       "                \"brushType\": \"stroke\",\n",
       "                \"scale\": 2.5,\n",
       "                \"period\": 4\n",
       "            }\n",
       "        },\n",
       "        {\n",
       "            \"type\": \"bar\",\n",
       "            \"name\": \"\\u516c\\u52a1\\u8231\",\n",
       "            \"legendHoverLink\": true,\n",
       "            \"data\": [\n",
       "                50408.0,\n",
       "                10714.0,\n",
       "                3315.0,\n",
       "                56567.0,\n",
       "                47581.0\n",
       "            ],\n",
       "            \"showBackground\": false,\n",
       "            \"barMinHeight\": 0,\n",
       "            \"barCategoryGap\": \"20%\",\n",
       "            \"barGap\": \"30%\",\n",
       "            \"large\": false,\n",
       "            \"largeThreshold\": 400,\n",
       "            \"seriesLayoutBy\": \"column\",\n",
       "            \"datasetIndex\": 0,\n",
       "            \"clip\": true,\n",
       "            \"zlevel\": 0,\n",
       "            \"z\": 2,\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"top\",\n",
       "                \"margin\": 8\n",
       "            },\n",
       "            \"rippleEffect\": {\n",
       "                \"show\": true,\n",
       "                \"brushType\": \"stroke\",\n",
       "                \"scale\": 2.5,\n",
       "                \"period\": 4\n",
       "            }\n",
       "        },\n",
       "        {\n",
       "            \"type\": \"bar\",\n",
       "            \"name\": \"\\u7ecf\\u6d4e\\u8231\",\n",
       "            \"legendHoverLink\": true,\n",
       "            \"data\": [\n",
       "                604719.0,\n",
       "                145010.0,\n",
       "                49482.0,\n",
       "                727642.0,\n",
       "                616207.0\n",
       "            ],\n",
       "            \"showBackground\": false,\n",
       "            \"barMinHeight\": 0,\n",
       "            \"barCategoryGap\": \"20%\",\n",
       "            \"barGap\": \"30%\",\n",
       "            \"large\": false,\n",
       "            \"largeThreshold\": 400,\n",
       "            \"seriesLayoutBy\": \"column\",\n",
       "            \"datasetIndex\": 0,\n",
       "            \"clip\": true,\n",
       "            \"zlevel\": 0,\n",
       "            \"z\": 2,\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"top\",\n",
       "                \"margin\": 8\n",
       "            },\n",
       "            \"rippleEffect\": {\n",
       "                \"show\": true,\n",
       "                \"brushType\": \"stroke\",\n",
       "                \"scale\": 2.5,\n",
       "                \"period\": 4\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"legend\": [\n",
       "        {\n",
       "            \"data\": [\n",
       "                \"\\u5934\\u7b49\\u8231\",\n",
       "                \"\\u516c\\u52a1\\u8231\",\n",
       "                \"\\u7ecf\\u6d4e\\u8231\"\n",
       "            ],\n",
       "            \"selected\": {\n",
       "                \"\\u5934\\u7b49\\u8231\": true,\n",
       "                \"\\u516c\\u52a1\\u8231\": true,\n",
       "                \"\\u7ecf\\u6d4e\\u8231\": true\n",
       "            },\n",
       "            \"show\": true,\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10,\n",
       "            \"itemWidth\": 25,\n",
       "            \"itemHeight\": 14\n",
       "        }\n",
       "    ],\n",
       "    \"tooltip\": {\n",
       "        \"show\": true,\n",
       "        \"trigger\": \"item\",\n",
       "        \"triggerOn\": \"mousemove|click\",\n",
       "        \"axisPointer\": {\n",
       "            \"type\": \"line\"\n",
       "        },\n",
       "        \"showContent\": true,\n",
       "        \"alwaysShowContent\": false,\n",
       "        \"showDelay\": 0,\n",
       "        \"hideDelay\": 100,\n",
       "        \"textStyle\": {\n",
       "            \"fontSize\": 14\n",
       "        },\n",
       "        \"borderWidth\": 0,\n",
       "        \"padding\": 5\n",
       "    },\n",
       "    \"xAxis\": [\n",
       "        {\n",
       "            \"show\": true,\n",
       "            \"scale\": false,\n",
       "            \"nameLocation\": \"end\",\n",
       "            \"nameGap\": 15,\n",
       "            \"gridIndex\": 0,\n",
       "            \"inverse\": false,\n",
       "            \"offset\": 0,\n",
       "            \"splitNumber\": 5,\n",
       "            \"minInterval\": 0,\n",
       "            \"splitLine\": {\n",
       "                \"show\": false,\n",
       "                \"lineStyle\": {\n",
       "                    \"show\": true,\n",
       "                    \"width\": 1,\n",
       "                    \"opacity\": 1,\n",
       "                    \"curveness\": 0,\n",
       "                    \"type\": \"solid\"\n",
       "                }\n",
       "            },\n",
       "            \"data\": [\n",
       "                \"1-2\\u7ea7\\u98ce\",\n",
       "                \"3-4\\u7ea7\\u98ce\",\n",
       "                \"4-5\\u7ea7\\u98ce\",\n",
       "                \"\\u5fae\\u98ce\",\n",
       "                \"\\u65e0\\u6301\\u7eed\\u98ce\\u5411\\u5fae\\u98ce\"\n",
       "            ]\n",
       "        }\n",
       "    ],\n",
       "    \"yAxis\": [\n",
       "        {\n",
       "            \"show\": true,\n",
       "            \"scale\": false,\n",
       "            \"nameLocation\": \"end\",\n",
       "            \"nameGap\": 15,\n",
       "            \"gridIndex\": 0,\n",
       "            \"inverse\": false,\n",
       "            \"offset\": 0,\n",
       "            \"splitNumber\": 5,\n",
       "            \"minInterval\": 0,\n",
       "            \"splitLine\": {\n",
       "                \"show\": false,\n",
       "                \"lineStyle\": {\n",
       "                    \"show\": true,\n",
       "                    \"width\": 1,\n",
       "                    \"opacity\": 1,\n",
       "                    \"curveness\": 0,\n",
       "                    \"type\": \"solid\"\n",
       "                }\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"title\": [\n",
       "        {\n",
       "            \"text\": \"\\u98ce\\u5bf9\\u4e0a\\u5ea7\\u7387\\u7684\\u5f71\\u54cd\",\n",
       "            \"subtext\": \"\\u98ce\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10\n",
       "        }\n",
       "    ],\n",
       "    \"toolbox\": {\n",
       "        \"show\": true,\n",
       "        \"orient\": \"horizontal\",\n",
       "        \"itemSize\": 15,\n",
       "        \"itemGap\": 10,\n",
       "        \"left\": \"80%\",\n",
       "        \"feature\": {\n",
       "            \"saveAsImage\": {\n",
       "                \"type\": \"png\",\n",
       "                \"backgroundColor\": \"auto\",\n",
       "                \"connectedBackgroundColor\": \"#fff\",\n",
       "                \"show\": true,\n",
       "                \"title\": \"\\u4fdd\\u5b58\\u4e3a\\u56fe\\u7247\",\n",
       "                \"pixelRatio\": 1\n",
       "            },\n",
       "            \"restore\": {\n",
       "                \"show\": true,\n",
       "                \"title\": \"\\u8fd8\\u539f\"\n",
       "            },\n",
       "            \"dataView\": {\n",
       "                \"show\": true,\n",
       "                \"title\": \"\\u6570\\u636e\\u89c6\\u56fe\",\n",
       "                \"readOnly\": false,\n",
       "                \"lang\": [\n",
       "                    \"\\u6570\\u636e\\u89c6\\u56fe\",\n",
       "                    \"\\u5173\\u95ed\",\n",
       "                    \"\\u5237\\u65b0\"\n",
       "                ],\n",
       "                \"backgroundColor\": \"#fff\",\n",
       "                \"textareaColor\": \"#fff\",\n",
       "                \"textareaBorderColor\": \"#333\",\n",
       "                \"textColor\": \"#000\",\n",
       "                \"buttonColor\": \"#c23531\",\n",
       "                \"buttonTextColor\": \"#fff\"\n",
       "            },\n",
       "            \"dataZoom\": {\n",
       "                \"show\": true,\n",
       "                \"title\": {\n",
       "                    \"zoom\": \"\\u533a\\u57df\\u7f29\\u653e\",\n",
       "                    \"back\": \"\\u533a\\u57df\\u7f29\\u653e\\u8fd8\\u539f\"\n",
       "                },\n",
       "                \"icon\": {},\n",
       "                \"xAxisIndex\": false,\n",
       "                \"yAxisIndex\": false,\n",
       "                \"filterMode\": \"filter\"\n",
       "            },\n",
       "            \"magicType\": {\n",
       "                \"show\": true,\n",
       "                \"type\": [\n",
       "                    \"line\",\n",
       "                    \"bar\",\n",
       "                    \"stack\",\n",
       "                    \"tiled\"\n",
       "                ],\n",
       "                \"title\": {\n",
       "                    \"line\": \"\\u5207\\u6362\\u4e3a\\u6298\\u7ebf\\u56fe\",\n",
       "                    \"bar\": \"\\u5207\\u6362\\u4e3a\\u67f1\\u72b6\\u56fe\",\n",
       "                    \"stack\": \"\\u5207\\u6362\\u4e3a\\u5806\\u53e0\",\n",
       "                    \"tiled\": \"\\u5207\\u6362\\u4e3a\\u5e73\\u94fa\"\n",
       "                },\n",
       "                \"icon\": {}\n",
       "            },\n",
       "            \"brush\": {\n",
       "                \"icon\": {},\n",
       "                \"title\": {\n",
       "                    \"rect\": \"\\u77e9\\u5f62\\u9009\\u62e9\",\n",
       "                    \"polygon\": \"\\u5708\\u9009\",\n",
       "                    \"lineX\": \"\\u6a2a\\u5411\\u9009\\u62e9\",\n",
       "                    \"lineY\": \"\\u7eb5\\u5411\\u9009\\u62e9\",\n",
       "                    \"keep\": \"\\u4fdd\\u6301\\u9009\\u62e9\",\n",
       "                    \"clear\": \"\\u6e05\\u9664\\u9009\\u62e9\"\n",
       "                }\n",
       "            }\n",
       "        }\n",
       "    }\n",
       "};\n",
       "                chart_870f13a2d0df45678425a462ef5a9ceb.setOption(option_870f13a2d0df45678425a462ef5a9ceb);\n",
       "        });\n",
       "    </script>\n"
      ],
      "text/plain": [
       "<pyecharts.render.display.HTML at 0x27b0d7d9588>"
      ]
     },
     "execution_count": 332,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pyecharts.charts import Bar\n",
    "from pyecharts import options as opts\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "\n",
    "\n",
    "bar = Bar()\n",
    "\n",
    "\n",
    "X_list = list(df_result2.index.values)\n",
    "y1_list = list(df_result2['头等舱'].values)\n",
    "y2_list = list(df_result2['公务舱'].values)\n",
    "y3_list = list(df_result2['经济舱'].values)\n",
    "\n",
    "\n",
    "# print(bar.options['color'])\n",
    "bar.add_xaxis(X_list)\n",
    "bar.add_yaxis('头等舱',y1_list,color='#228B22')\n",
    "bar.add_yaxis('公务舱',y2_list,color='#FFDAB9')\n",
    "bar.add_yaxis('经济舱',y3_list,color='#CD5C5C')\n",
    "\n",
    "bar.set_global_opts(title_opts=opts.TitleOpts(title=\"风对上座率的影响\",subtitle='风'),\n",
    "                   toolbox_opts=opts.ToolboxOpts(is_show=True))\n",
    "bar.set_series_opts(label_opts=opts.LabelOpts(position=\"top\"))\n",
    "bar.render_notebook()    # 在 notebook 中展示\n",
    "# bar.render(r\"snapshot.html\") 生成 html 文件\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 333,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<script>\n",
       "    require.config({\n",
       "        paths: {\n",
       "            'echarts':'https://assets.pyecharts.org/assets/echarts.min'\n",
       "        }\n",
       "    });\n",
       "</script>\n",
       "\n",
       "        <div id=\"a5e6bb49cefe4ccc8431ef78b68b0173\" style=\"width:900px; height:500px;\"></div>\n",
       "\n",
       "<script>\n",
       "        require(['echarts'], function(echarts) {\n",
       "                var chart_a5e6bb49cefe4ccc8431ef78b68b0173 = echarts.init(\n",
       "                    document.getElementById('a5e6bb49cefe4ccc8431ef78b68b0173'), 'white', {renderer: 'canvas'});\n",
       "                var option_a5e6bb49cefe4ccc8431ef78b68b0173 = {\n",
       "    \"animation\": true,\n",
       "    \"animationThreshold\": 2000,\n",
       "    \"animationDuration\": 1000,\n",
       "    \"animationEasing\": \"cubicOut\",\n",
       "    \"animationDelay\": 0,\n",
       "    \"animationDurationUpdate\": 300,\n",
       "    \"animationEasingUpdate\": \"cubicOut\",\n",
       "    \"animationDelayUpdate\": 0,\n",
       "    \"color\": [\n",
       "        \"#749f83\",\n",
       "        \"#87CEEB   \",\n",
       "        \"#c23531\",\n",
       "        \"#2f4554\",\n",
       "        \"#61a0a8\",\n",
       "        \"#d48265\",\n",
       "        \"#749f83\",\n",
       "        \"#ca8622\",\n",
       "        \"#bda29a\",\n",
       "        \"#6e7074\",\n",
       "        \"#546570\",\n",
       "        \"#c4ccd3\",\n",
       "        \"#f05b72\",\n",
       "        \"#ef5b9c\",\n",
       "        \"#f47920\",\n",
       "        \"#905a3d\",\n",
       "        \"#fab27b\",\n",
       "        \"#2a5caa\",\n",
       "        \"#444693\",\n",
       "        \"#726930\",\n",
       "        \"#b2d235\",\n",
       "        \"#6d8346\",\n",
       "        \"#ac6767\",\n",
       "        \"#1d953f\",\n",
       "        \"#6950a1\",\n",
       "        \"#918597\"\n",
       "    ],\n",
       "    \"series\": [\n",
       "        {\n",
       "            \"type\": \"bar\",\n",
       "            \"name\": \"\\u5176\\u4ed6\",\n",
       "            \"legendHoverLink\": true,\n",
       "            \"data\": [\n",
       "                750437.0,\n",
       "                177848.0,\n",
       "                61110.0,\n",
       "                895779.0,\n",
       "                764800.0\n",
       "            ],\n",
       "            \"showBackground\": false,\n",
       "            \"barMinHeight\": 0,\n",
       "            \"barCategoryGap\": \"20%\",\n",
       "            \"barGap\": \"30%\",\n",
       "            \"large\": false,\n",
       "            \"largeThreshold\": 400,\n",
       "            \"seriesLayoutBy\": \"column\",\n",
       "            \"datasetIndex\": 0,\n",
       "            \"clip\": true,\n",
       "            \"zlevel\": 0,\n",
       "            \"z\": 2,\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"top\",\n",
       "                \"margin\": 8\n",
       "            },\n",
       "            \"rippleEffect\": {\n",
       "                \"show\": true,\n",
       "                \"brushType\": \"stroke\",\n",
       "                \"scale\": 2.5,\n",
       "                \"period\": 4\n",
       "            }\n",
       "        },\n",
       "        {\n",
       "            \"type\": \"bar\",\n",
       "            \"name\": \"\\u603b\\u6570\",\n",
       "            \"legendHoverLink\": true,\n",
       "            \"data\": [\n",
       "                31820154.0,\n",
       "                8174133.0,\n",
       "                2921156.0,\n",
       "                41845498.0,\n",
       "                33723218.0\n",
       "            ],\n",
       "            \"showBackground\": false,\n",
       "            \"barMinHeight\": 0,\n",
       "            \"barCategoryGap\": \"20%\",\n",
       "            \"barGap\": \"30%\",\n",
       "            \"large\": false,\n",
       "            \"largeThreshold\": 400,\n",
       "            \"seriesLayoutBy\": \"column\",\n",
       "            \"datasetIndex\": 0,\n",
       "            \"clip\": true,\n",
       "            \"zlevel\": 0,\n",
       "            \"z\": 2,\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"top\",\n",
       "                \"margin\": 8\n",
       "            },\n",
       "            \"rippleEffect\": {\n",
       "                \"show\": true,\n",
       "                \"brushType\": \"stroke\",\n",
       "                \"scale\": 2.5,\n",
       "                \"period\": 4\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"legend\": [\n",
       "        {\n",
       "            \"data\": [\n",
       "                \"\\u5176\\u4ed6\",\n",
       "                \"\\u603b\\u6570\"\n",
       "            ],\n",
       "            \"selected\": {\n",
       "                \"\\u5176\\u4ed6\": true,\n",
       "                \"\\u603b\\u6570\": true\n",
       "            },\n",
       "            \"show\": true,\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10,\n",
       "            \"itemWidth\": 25,\n",
       "            \"itemHeight\": 14\n",
       "        }\n",
       "    ],\n",
       "    \"tooltip\": {\n",
       "        \"show\": true,\n",
       "        \"trigger\": \"item\",\n",
       "        \"triggerOn\": \"mousemove|click\",\n",
       "        \"axisPointer\": {\n",
       "            \"type\": \"line\"\n",
       "        },\n",
       "        \"showContent\": true,\n",
       "        \"alwaysShowContent\": false,\n",
       "        \"showDelay\": 0,\n",
       "        \"hideDelay\": 100,\n",
       "        \"textStyle\": {\n",
       "            \"fontSize\": 14\n",
       "        },\n",
       "        \"borderWidth\": 0,\n",
       "        \"padding\": 5\n",
       "    },\n",
       "    \"xAxis\": [\n",
       "        {\n",
       "            \"show\": true,\n",
       "            \"scale\": false,\n",
       "            \"nameLocation\": \"end\",\n",
       "            \"nameGap\": 15,\n",
       "            \"gridIndex\": 0,\n",
       "            \"inverse\": false,\n",
       "            \"offset\": 0,\n",
       "            \"splitNumber\": 5,\n",
       "            \"minInterval\": 0,\n",
       "            \"splitLine\": {\n",
       "                \"show\": false,\n",
       "                \"lineStyle\": {\n",
       "                    \"show\": true,\n",
       "                    \"width\": 1,\n",
       "                    \"opacity\": 1,\n",
       "                    \"curveness\": 0,\n",
       "                    \"type\": \"solid\"\n",
       "                }\n",
       "            },\n",
       "            \"data\": [\n",
       "                \"1-2\\u7ea7\\u98ce\",\n",
       "                \"3-4\\u7ea7\\u98ce\",\n",
       "                \"4-5\\u7ea7\\u98ce\",\n",
       "                \"\\u5fae\\u98ce\",\n",
       "                \"\\u65e0\\u6301\\u7eed\\u98ce\\u5411\\u5fae\\u98ce\"\n",
       "            ]\n",
       "        }\n",
       "    ],\n",
       "    \"yAxis\": [\n",
       "        {\n",
       "            \"show\": true,\n",
       "            \"scale\": false,\n",
       "            \"nameLocation\": \"end\",\n",
       "            \"nameGap\": 15,\n",
       "            \"gridIndex\": 0,\n",
       "            \"inverse\": false,\n",
       "            \"offset\": 0,\n",
       "            \"splitNumber\": 5,\n",
       "            \"minInterval\": 0,\n",
       "            \"splitLine\": {\n",
       "                \"show\": false,\n",
       "                \"lineStyle\": {\n",
       "                    \"show\": true,\n",
       "                    \"width\": 1,\n",
       "                    \"opacity\": 1,\n",
       "                    \"curveness\": 0,\n",
       "                    \"type\": \"solid\"\n",
       "                }\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"title\": [\n",
       "        {\n",
       "            \"text\": \"\\u98ce\\u5bf9\\u4e0a\\u5ea7\\u7387\\u7684\\u5f71\\u54cd\",\n",
       "            \"subtext\": \"\\u98ce\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10\n",
       "        }\n",
       "    ],\n",
       "    \"toolbox\": {\n",
       "        \"show\": true,\n",
       "        \"orient\": \"horizontal\",\n",
       "        \"itemSize\": 15,\n",
       "        \"itemGap\": 10,\n",
       "        \"left\": \"80%\",\n",
       "        \"feature\": {\n",
       "            \"saveAsImage\": {\n",
       "                \"type\": \"png\",\n",
       "                \"backgroundColor\": \"auto\",\n",
       "                \"connectedBackgroundColor\": \"#fff\",\n",
       "                \"show\": true,\n",
       "                \"title\": \"\\u4fdd\\u5b58\\u4e3a\\u56fe\\u7247\",\n",
       "                \"pixelRatio\": 1\n",
       "            },\n",
       "            \"restore\": {\n",
       "                \"show\": true,\n",
       "                \"title\": \"\\u8fd8\\u539f\"\n",
       "            },\n",
       "            \"dataView\": {\n",
       "                \"show\": true,\n",
       "                \"title\": \"\\u6570\\u636e\\u89c6\\u56fe\",\n",
       "                \"readOnly\": false,\n",
       "                \"lang\": [\n",
       "                    \"\\u6570\\u636e\\u89c6\\u56fe\",\n",
       "                    \"\\u5173\\u95ed\",\n",
       "                    \"\\u5237\\u65b0\"\n",
       "                ],\n",
       "                \"backgroundColor\": \"#fff\",\n",
       "                \"textareaColor\": \"#fff\",\n",
       "                \"textareaBorderColor\": \"#333\",\n",
       "                \"textColor\": \"#000\",\n",
       "                \"buttonColor\": \"#c23531\",\n",
       "                \"buttonTextColor\": \"#fff\"\n",
       "            },\n",
       "            \"dataZoom\": {\n",
       "                \"show\": true,\n",
       "                \"title\": {\n",
       "                    \"zoom\": \"\\u533a\\u57df\\u7f29\\u653e\",\n",
       "                    \"back\": \"\\u533a\\u57df\\u7f29\\u653e\\u8fd8\\u539f\"\n",
       "                },\n",
       "                \"icon\": {},\n",
       "                \"xAxisIndex\": false,\n",
       "                \"yAxisIndex\": false,\n",
       "                \"filterMode\": \"filter\"\n",
       "            },\n",
       "            \"magicType\": {\n",
       "                \"show\": true,\n",
       "                \"type\": [\n",
       "                    \"line\",\n",
       "                    \"bar\",\n",
       "                    \"stack\",\n",
       "                    \"tiled\"\n",
       "                ],\n",
       "                \"title\": {\n",
       "                    \"line\": \"\\u5207\\u6362\\u4e3a\\u6298\\u7ebf\\u56fe\",\n",
       "                    \"bar\": \"\\u5207\\u6362\\u4e3a\\u67f1\\u72b6\\u56fe\",\n",
       "                    \"stack\": \"\\u5207\\u6362\\u4e3a\\u5806\\u53e0\",\n",
       "                    \"tiled\": \"\\u5207\\u6362\\u4e3a\\u5e73\\u94fa\"\n",
       "                },\n",
       "                \"icon\": {}\n",
       "            },\n",
       "            \"brush\": {\n",
       "                \"icon\": {},\n",
       "                \"title\": {\n",
       "                    \"rect\": \"\\u77e9\\u5f62\\u9009\\u62e9\",\n",
       "                    \"polygon\": \"\\u5708\\u9009\",\n",
       "                    \"lineX\": \"\\u6a2a\\u5411\\u9009\\u62e9\",\n",
       "                    \"lineY\": \"\\u7eb5\\u5411\\u9009\\u62e9\",\n",
       "                    \"keep\": \"\\u4fdd\\u6301\\u9009\\u62e9\",\n",
       "                    \"clear\": \"\\u6e05\\u9664\\u9009\\u62e9\"\n",
       "                }\n",
       "            }\n",
       "        }\n",
       "    }\n",
       "};\n",
       "                chart_a5e6bb49cefe4ccc8431ef78b68b0173.setOption(option_a5e6bb49cefe4ccc8431ef78b68b0173);\n",
       "        });\n",
       "    </script>\n"
      ],
      "text/plain": [
       "<pyecharts.render.display.HTML at 0x27b264ad048>"
      ]
     },
     "execution_count": 333,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bar1 = Bar()\n",
    "\n",
    "con1=df_result2['头等舱'].values+df_result2['公务舱'].values+df_result2['经济舱'].values+df_result2['其他'].values\n",
    "con2=df_result2['头等舱总数'].values+df_result2['公务舱总数'].values+df_result2['经济舱总数'].values+df_result2['其他总数'].values\n",
    "con1_list=list(con1)\n",
    "con2_list=list(con2)\n",
    "bar1.add_xaxis(X_list)\n",
    "bar1.add_yaxis('其他',con1_list,color='#87CEEB   ')\n",
    "bar1.add_yaxis('总数',con2_list,color='#749f83')\n",
    "\n",
    "bar1.set_global_opts(title_opts=opts.TitleOpts(title=\"风对上座率的影响\",subtitle='风')\n",
    "                     ,toolbox_opts=opts.ToolboxOpts(is_show=True))\n",
    "bar1.set_series_opts(label_opts=opts.LabelOpts(position=\"top\",is_show=True))\n",
    "bar1.render_notebook()    # 在 notebook 中展示\n",
    "# print(bar.options['color'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- ## 4、数据分析建模并评估"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 334,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>天气状况</th>\n",
       "      <th>高温</th>\n",
       "      <th>低温</th>\n",
       "      <th>上座率</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>23.0</td>\n",
       "      <td>21.0</td>\n",
       "      <td>0.40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>25.0</td>\n",
       "      <td>22.0</td>\n",
       "      <td>0.40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>33.0</td>\n",
       "      <td>24.0</td>\n",
       "      <td>0.42</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>34.0</td>\n",
       "      <td>24.0</td>\n",
       "      <td>0.42</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>-6.0</td>\n",
       "      <td>-13.0</td>\n",
       "      <td>0.38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>321</td>\n",
       "      <td>321</td>\n",
       "      <td>10</td>\n",
       "      <td>23.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>0.38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>322</td>\n",
       "      <td>322</td>\n",
       "      <td>10</td>\n",
       "      <td>25.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>0.48</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>323</td>\n",
       "      <td>323</td>\n",
       "      <td>10</td>\n",
       "      <td>26.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>0.48</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>324</td>\n",
       "      <td>324</td>\n",
       "      <td>10</td>\n",
       "      <td>27.0</td>\n",
       "      <td>19.0</td>\n",
       "      <td>0.41</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>325</td>\n",
       "      <td>325</td>\n",
       "      <td>10</td>\n",
       "      <td>29.0</td>\n",
       "      <td>15.0</td>\n",
       "      <td>0.37</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>326 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     Unnamed: 0  天气状况  高温  低温  上座率\n",
       "0             0         0  23.0  21.0    0.40\n",
       "1             1         0  25.0  22.0    0.40\n",
       "2             2         0  33.0  24.0    0.42\n",
       "3             3         0  34.0  24.0    0.42\n",
       "4             4         1  -6.0 -13.0    0.38\n",
       "..          ...       ...   ...   ...     ...\n",
       "321         321        10  23.0  13.0    0.38\n",
       "322         322        10  25.0  16.0    0.48\n",
       "323         323        10  26.0  16.0    0.48\n",
       "324         324        10  27.0  19.0    0.41\n",
       "325         325        10  29.0  15.0    0.37\n",
       "\n",
       "[326 rows x 5 columns]"
      ]
     },
     "execution_count": 334,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# knn回归 KNeighborsRegressor模型\n",
    "import pandas as pd\n",
    "pd.set_option('display.unicode.east_asian_width',True)\n",
    "import numpy as np\n",
    "from sklearn.neighbors import KNeighborsRegressor\n",
    "from sklearn.model_selection import train_test_split  #分割数据\n",
    "from sklearn.preprocessing import StandardScaler  #用于数据预加工标准化\n",
    "from sklearn.linear_model import LogisticRegression     # 线性模型中的Logistic回归模型\n",
    "# from sklearn.neural_network import MLPClassifier        # 神经网络模型中的多层网络模型\n",
    "from sklearn.svm import LinearSVC                       # SVM模型中的线性SVC模型\n",
    "# from sklearn.linear_model import SGDClassifier          # 线性模型中的随机梯度下降模型\n",
    "data=pd.read_csv('根据天气状况特征预测.csv')\n",
    "# print(data)\n",
    "# X=pd.get_dummies(data['天气状况'])\n",
    "data=data.replace(['中雨','多云','大雪','小雨','晴','阴','阵雨','雨夹雪','雷阵雨','雾','霾'],[0,1,2,3,4,5,6,7,8,9,10])\n",
    "X=data.iloc[:,1:4]\n",
    "y=data.iloc[:,4]\n",
    "# X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.3)\n",
    "# knn=KNeighborsRegressor(n_neighbors=5,n_jobs=-1)\n",
    "# knn.fit(X_train,y_train)\n",
    "# y_pred=knn.predict(X_test)\n",
    "# print(y_test[:10].ravel())\n",
    "# print(y_pred[:10])\n",
    "# print(knn.score(X_test,y_test))\n",
    "# print((y_pred==y_test))\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 243,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((228, 3), (98, 3))"
      ]
     },
     "execution_count": 243,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# import pandas as pd\n",
    "# pd.set_option('display.unicode.east_asian_width',True)\n",
    "# import numpy as np\n",
    "# from sklearn.neighbors import KNeighborsRegressor\n",
    "# from sklearn.model_selection import train_test_split  #分割数据\n",
    "# from sklearn.preprocessing import StandardScaler  #用于数据预加工标准化\n",
    "# from sklearn.preprocessing import MinMaxScaler\n",
    "# from sklearn.linear_model import LogisticRegression     # 线性模型中的Logistic回归模型\n",
    "# # from sklearn.neural_network import MLPClassifier        # 神经网络模型中的多层网络模型\n",
    "# from sklearn.svm import LinearSVC                       # SVM模型中的线性SVC模型\n",
    "# # from sklearn.linear_model import SGDClassifier          # 线性模型中的随机梯度下降模型\n",
    "\n",
    "\n",
    "# data=pd.read_csv('根据天气状况特征预测.csv')\n",
    "\n",
    "\n",
    "# data=data.replace(['中雨','多云','大雪','小雨','晴','阴','阵雨','雨夹雪','雷阵雨','雾','霾'],[0,1,2,3,4,5,6,7,8,9,10])\n",
    "# X=data.iloc[:,1:4]\n",
    "# y=data.iloc[:,4]\n",
    "# X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.3)\n",
    "\n",
    "\n",
    "# #离差标准化\n",
    "# Scaler = MinMaxScaler().fit(X_train)\n",
    "# X_train = Scaler.transform(X_train)\n",
    "# X_test = Scaler.transform(X_test)\n",
    "\n",
    "# X_train.shape,X_test.shape\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 286,
   "metadata": {},
   "outputs": [],
   "source": [
    "# from sklearn.linear_model import LogisticRegression,LinearRegression,Ridge,Lasso\n",
    "# import matplotlib.pyplot as plt\n",
    "# plt.rcParams['font.sans-serif'] = ['SimHei']#用来正常显示中文标签\n",
    "# plt.rcParams['axes.unicode_minus'] = False#用来正常显示负号\n",
    "\n",
    "\n",
    "# # #linear_model模块调用逻辑回归模型\n",
    "# # lr = LogisticRegression(C=100)\n",
    "# # lr.fit(X_train, y_train)\n",
    "# # y_pre_lr = lr.predict(X_test)#使用测试数据，获取预测结果\n",
    "# # print(\"逻辑回归:\")\n",
    "# # print(\"Training set score: {:.3f}\".format(lr.score(X_train, y_train)))\n",
    "# # print(\"Testing set score: {:.3f}\".format(lr.score(X_test, y_test)))\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "# #普通线性回归模型\n",
    "# line=LinearRegression()\n",
    "# line.fit(X_train,y_train)\n",
    "# y_pre_line = line.predict(X_test)#使用测试数据，获取预测结果\n",
    "# print(\"普通线性回归:\")\n",
    "# print(\"Training set score: {:.3f}\".format(line.score(X_train, y_train)))\n",
    "# print(\"Testing set score: {:.3f}\".format(line.score(X_test, y_test)))\n",
    "\n",
    "\n",
    "# #岭回归模型\n",
    "# ridge = Ridge(alpha=0.5)\n",
    "# ridge.fit(X_train,y_train)\n",
    "# y_pre_ridge = ridge.predict(X_test)#使用测试数据，获取预测结果\n",
    "# print(\"岭回归:\")\n",
    "# print(\"Training set score: {:.3f}\".format(ridge.score(X_train, y_train)))\n",
    "# print(\"Testing set score: {:.3f}\".format(ridge.score(X_test, y_test)))\n",
    "\n",
    "\n",
    "# #lasso回归模型\n",
    "# lasso = Lasso(alpha=0.003)\n",
    "# lasso.fit(X_train,y_train)\n",
    "# y_pre_lasso = line.predict(X_test)#使用测试数据，获取预测结果\n",
    "# print(\"lasso:\")\n",
    "# print(\"Training set score: {:.3f}\".format(lasso.score(X_train, y_train)))\n",
    "# print(\"Testing set score: {:.3f}\".format(lasso.score(X_test, y_test)))\n",
    "\n",
    "\n",
    "# #普通线性回归\n",
    "# plt.plot(np.arange(y_pre_line.size),y_pre_line)\n",
    "# plt.plot(np.arange(y_test.size),y_test)\n",
    "\n",
    "# plt.title('普通线性回归')\n",
    "# plt.show()\n",
    "\n",
    "# #岭回归模型\n",
    "# plt.plot(np.arange(y_pre_ridge.size),y_pre_ridge,color='orange')\n",
    "# plt.plot(np.arange(y_test.size),y_test,color='green')\n",
    "\n",
    "# plt.title('岭回归模型')\n",
    "# plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 295,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=======================决策树算法=========================\n",
      "决策树预测的上座率为： [[0.19503769 0.23343024 0.22880916 0.19262495 0.19223471]\n",
      " [0.19455728 0.24828516 0.26923549 0.18978284 0.19113119]\n",
      " [0.19493351 0.24368134 0.2298451  0.19250924 0.19234484]\n",
      " [0.19503769 0.23343024 0.22880916 0.19262495 0.19223471]\n",
      " [0.19493351 0.24368134 0.2298451  0.19250924 0.19234484]]\n",
      "实际的上座率为： [[0.1933 0.2308 0.2355 0.1855 0.1878]\n",
      " [0.1948 0.2437 0.2637 0.1855 0.1978]\n",
      " [0.1946 0.2462 0.2355 0.1944 0.1978]\n",
      " [0.1956 0.2366 0.2272 0.196  0.1907]\n",
      " [0.1933 0.2422 0.2355 0.1855 0.1907]]\n",
      "决策树平均准确度： 0.4720822690232385\n",
      "=======================随机森林算法=========================\n",
      "随机森林预测的上座率为： [[0.19503758 0.23342908 0.22880874 0.19262351 0.19223511]\n",
      " [0.19455746 0.2482878  0.26923744 0.18978386 0.19113035]\n",
      " [0.19493374 0.24368244 0.22984336 0.19251026 0.19234467]\n",
      " [0.19503758 0.23342908 0.22880874 0.19262351 0.19223511]\n",
      " [0.19493374 0.24368244 0.22984336 0.19251026 0.19234467]]\n",
      "实际的的上座率为： [[0.1933 0.2308 0.2355 0.1855 0.1878]\n",
      " [0.1948 0.2437 0.2637 0.1855 0.1978]\n",
      " [0.1946 0.2462 0.2355 0.1944 0.1978]\n",
      " [0.1956 0.2366 0.2272 0.196  0.1907]\n",
      " [0.1933 0.2422 0.2355 0.1855 0.1907]]\n",
      "随机森林平均准确度： 0.4720817505012437\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "from sklearn.neighbors import KNeighborsRegressor\n",
    "from sklearn.metrics import mean_squared_error,mean_absolute_error  # 评价指标\n",
    "from sklearn.model_selection import cross_val_score\n",
    "from sklearn.tree import DecisionTreeRegressor\n",
    "data = pd.read_csv('特征数据.csv')\n",
    "#data = data.replace(['中雨','多云','大雪','小雨','晴','阴','阵雨','雨夹雪','雷阵雨','雾','霾'],[0,1,2,3,4,5,6,7,8,9,10])\n",
    "for i in range(7,13):\n",
    "    data.iloc[:,i] = data.iloc[:,i] .apply(lambda x: format(x, '.4'))\n",
    "    data.iloc[:,i] = data.iloc[:,i].astype(float)\n",
    "#data.iloc[:,12] = data.iloc[:,12] .apply(lambda x: format(x, '.4'))\n",
    "#data.iloc[:,12] = data.iloc[:,12].astype(str)\n",
    "data = data.drop(['Unnamed: 0','航班号','高温','低温','天气状况','风','空气'],axis=1)\n",
    "x_train, x_test, y_train, y_test = train_test_split(np.array(data.iloc[:,:-1]),np.array(data.iloc[:,:-1]),test_size=0.3)\n",
    "#离差标准化\n",
    "Scaler = MinMaxScaler().fit(x_train)\n",
    "x_train = Scaler.transform(x_train)\n",
    "x_test = Scaler.transform(x_test)\n",
    "\n",
    "\n",
    "#决策树\n",
    "from sklearn import tree#导入模块\n",
    "from sklearn.tree import DecisionTreeRegressor\n",
    "from sklearn.ensemble import RandomForestRegressor\n",
    "print(\"=======================决策树算法=========================\")\n",
    "tree = DecisionTreeRegressor(max_depth=3).fit(x_train, y_train)\n",
    "pred_tree = tree.predict(x_test)\n",
    "print(\"决策树预测的上座率为：\",pred_tree[:5])\n",
    "print(\"实际的上座率为：\",y_test[:5])\n",
    "print(\"决策树平均准确度：\",tree.score(x_test,y_test))\n",
    "\n",
    "\n",
    "#随机森林\n",
    "print(\"=======================随机森林算法=========================\")\n",
    "tree = RandomForestRegressor(max_depth=3).fit(x_train, y_train)\n",
    "pred_tree = tree.predict(x_test)\n",
    "print(\"随机森林预测的上座率为：\",pred_tree[:5])\n",
    "print(\"实际的的上座率为：\",y_test[:5])\n",
    "print(\"随机森林平均准确度：\",tree.score(x_test,y_test))\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 330,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=======================lasso回归模型=========================\n",
      "lasso回归的上座率为： [[0.19486779 0.232264   0.23052029 0.19086542 0.19088209]\n",
      " [0.19486779 0.24156706 0.25670416 0.19086542 0.19088209]\n",
      " [0.19486779 0.24336997 0.23052029 0.19086542 0.19088209]\n",
      " [0.19486779 0.23644677 0.22281369 0.19086542 0.19088209]\n",
      " [0.19486779 0.24048531 0.23052029 0.19086542 0.19088209]]\n",
      "lasso回归的上座率为： [[0.1933 0.2308 0.2355 0.1855 0.1878]\n",
      " [0.1948 0.2437 0.2637 0.1855 0.1978]\n",
      " [0.1946 0.2462 0.2355 0.1944 0.1978]\n",
      " [0.1956 0.2366 0.2272 0.196  0.1907]\n",
      " [0.1933 0.2422 0.2355 0.1855 0.1907]]\n",
      "lasso回归的准确度： 0.3834263419339071\n"
     ]
    }
   ],
   "source": [
    "#lasso回归模型\n",
    "print(\"=======================lasso回归模型=========================\")\n",
    "lasso = Lasso(alpha=0.003)\n",
    "lasso.fit(x_train,y_train)\n",
    "y_pre_lasso = lasso.predict(x_test)#使用测试数据，获取预测结果\n",
    "\n",
    "print(\"lasso回归的上座率为：\",y_pre_lasso[:5])\n",
    "print(\"lasso回归的上座率为：\",y_test[:5])\n",
    "print(\"lasso回归的准确度：\",lasso.score(x_test,y_test))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- ## 5、整理工程并部署：输入航班号、根据未来一周的天气（网上爬取的天气预报），预测并输出其上座率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#普通线性回归模型\n",
    "print(\"=======================普通线性回归模型=========================\")\n",
    "line=LinearRegression()\n",
    "line.fit(x_train,y_train)\n",
    "y_pre_line = line.predict(x_test)#使用测试数据，获取预测结果\n",
    "print(\"普通线性回归的上座率为：\",y_pre_line[:5])\n",
    "print(\"实际的上座率为：\",y_test[:5])\n",
    "print(\"普通线性回归准确度：\",line.score(x_test,y_test))\n",
    "\n",
    "#岭回归模型\n",
    "ridge = Ridge(alpha=0.3)\n",
    "ridge.fit(x_train,y_train)\n",
    "y_pre_ridge = ridge.predict(x_test)#使用测试数据，获取预测结果\n",
    "print(\"岭回归回归的上座率为：\",y_pre_ridge[:5])\n",
    "print(\"岭回归的上座率为：\",y_test[:5])\n",
    "print(\"岭回归回归准确度：\",ridge.score(x_test,y_test))\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "vscode": {
   "interpreter": {
    "hash": "49cb93f377a7abe7414b7b0f21fb3017538004a126cf690fb524202736b7fb92"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
